<?php

namespace app\home\controller;

use think\Controller;
use think\Request;
use  think\Db;

class Shouye extends Controller
{
    public function shouye(Request $request)
    {

        //七牛域名
        $qiniu_config=cmf_get_option('qiniu');
        // 七牛样式处理 调整图片清晰度和比例 HYF
        $q_thumb = "?imageMogr2/auto-orient/thumbnail/!320x200r/gravity/Center/crop/320x200/quality/100/interlace/0";
        $this->assign('qdm',$qiniu_config['domain'].'/');

        //轮播图
        $slides = get_slide(14);
        $this->assign('slide', $slides);
        $this->assign('pic_start', $slides[0]);
        $this->assign('pic_end', $slides[count($slides) - 1]);


        //首页案例
        $data = Db::name('cases')
            ->where('remen',1)
            ->order('id','desc')
            ->limit(3)->select();

        //首页学装修
        $res = Db::name('article_manage')
            ->where('tuijian',1)
            ->order('id','desc')
            ->limit(3)->select();

        //直播厅
        $pro = Db::name("project")->field('id,address,area,start_time,project_picture')
            ->order('id', 'desc')
            ->limit(3)
            ->select();
        foreach ($pro as $index => $re) {
            $cli = Db::name('project_client')->field("name")->where('project_id', $re['id'])->select();
            $pro[$index]["client"] = $cli;
        }

        foreach ($pro as $index => $re) {
            $pro[$index]['project_picture'] = $qiniu_config['domain'] . '/' . $re['project_picture'] . $q_thumb;
        }

        $this->assign('res',$res);
        $this->assign('data',$data);
        $this->assign('pro',$pro);

        $weixin = new  Weixin();
        $url = request()->domain().$this->request->url();
        $js_config = $weixin->js_config($url);
        $this->assign('jsconfig',$js_config);
        return $this->fetch('shouye');
    }

    //学装修
    public function xzx(Request $request)
    {
//        $this->success('ok', '', $data);
        if ($request->isAjax()){
            $status[0] = '禁用';
            $status[1] = '正常';
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 5;
            $flag1 = $this->request->param('flag');
            if ( $flag1 == '0' ){
                $res = Db::name('article_manage')
                    ->where('fenlei' ,'装修些事')
                    ->order('id','desc')
                    ->page($page, $limit)
                    ->select();
                $qiniu_config = cmf_get_option('qiniu');
                // 七牛样式处理
                $q_thumb = "?imageMogr2/auto-orient/thumbnail/!180x180r/gravity/Center/crop/180x180/quality/100/interlace/0";
                foreach ($res as $index => $re) {
                    $res[$index]['small_img'] = $qiniu_config['domain'] . '/' . $re['small_img'];

                }
                return json(['code' => 1, 'msg' => 'ok', 'count' => count($res), 'data' => $res]);
            }

            else if ( $flag1 == '1' ){
                $res = Db::name('article_manage')
                    ->where('fenlei' ,'装修笔记')
                    ->order('id','desc')
                    ->page($page, $limit)
                    ->select();
                $qiniu_config = cmf_get_option('qiniu');
                // 七牛样式处理
                $q_thumb = "?imageMogr2/auto-orient/thumbnail/!180x180r/gravity/Center/crop/180x180/quality/100/interlace/0";
                foreach ($res as $index => $re) {
                    $res[$index]['small_img'] = $qiniu_config['domain'] . '/' . $re['small_img'] ;

                }
                return json(['code' => 1, 'msg' => 'ok', 'count' => count($res), 'data' => $res]);
            }

            else if ($flag1 == '2'){
                $res = Db::name('article_manage')
                    ->where('fenlei' ,"家装知识")
                    ->order('id','desc')
                    ->page($page, $limit)
                    ->select();
                $qiniu_config = cmf_get_option('qiniu');
                // 七牛样式处理
                $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/crop/60x60/quality/100/interlace/0";
                foreach ($res as $index => $re) {
                    $res[$index]['small_img'] = $qiniu_config['domain'] . '/' . $re['small_img'];

                }
                return json(['code' => 1, 'msg' => 'ok', 'count' => count($res), 'data' => $res]);
            } else if ( $flag1 == '3'){
                $res = Db::name('article_manage')
                    ->where('fenlei' ,'常见问题')
                    ->order('id','desc')
                    ->page($page, $limit)
                    ->select();
//                dump($res);
                $qiniu_config = cmf_get_option('qiniu');
                // 七牛样式处理
                $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/crop/60x60/quality/100/interlace/0";
                foreach ($res as $index => $re) {
                    $res[$index]['small_img'] = $qiniu_config['domain'] . '/' . $re['small_img'];

                }
                return json(['code' => 1, 'msg' => 'ok', 'count' => count($res), 'data' => $res]);
            }else if ( $flag1 == '4'){
                $res = Db::name('article_manage')
                    ->where('fenlei' ,'热门')
                    ->order('id','desc')
                    ->page($page, $limit)
                    ->select();
                $qiniu_config = cmf_get_option('qiniu');
                // 七牛样式处理
                $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/crop/60x60/quality/100/interlace/0";
                foreach ($res as $index => $re) {
                    $res[$index]['small_img'] = $qiniu_config['domain'] . '/' . $re['small_img'];

                }
                return json(['code' => 1, 'msg' => 'ok', 'count' => count($res), 'data' => $res]);
            }
            else {
                $res = Db::name('article_manage')
                    ->where('fenlei' ,'热门')
                    ->order('id','desc')
                    ->page($page, $limit)
                    ->select();
                $qiniu_config = cmf_get_option('qiniu');
                // 七牛样式处理
                $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/crop/60x60/quality/100/interlace/0";
                foreach ($res as $index => $re) {
                    $res[$index]['small_img'] = $qiniu_config['domain'] . '/' . $re['small_img'];

                }
                return json(['code' => 1, 'msg' => 'ok', 'count' => count($res), 'data' => $res]);
            }

        }
        return  $this->fetch('xzx');
    }

    //在线预约
    public function  onlineorder(){

       if($this->request->isAjax()){

           if(empty($ip)){
               $ip=  $this->request->ip();
           }
           $re = @file_get_contents('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=',$ip);
           if(empty($re)){ return false; }
           $jsonMatches = array();
           preg_match('#\{.+?\}#', $re, $jsonMatches);
           if(!isset($jsonMatches[0])){ return false; }
           $json = json_decode($jsonMatches[0], true);
           if(isset($json['ret']) && $json['ret'] == 1){
               $json['ip'] = $ip;
               unset($json['ret']);
           }else{
               return false;
           }
           $data = input('');
           $data['province']=$json['province'];
           $data['city']=$json['city'];

           $data['create_time'] = time();
           $data['status']=0;
           if($data !=  ''){
               $res = Db::name('yuyue')->insertGetId($data);
               if($res){
                   $this->success('操作成功');
               }

           }

//           $res=["name"=>"","phone"=>"","create_time"=>""];
//               $res["name"]=$data["name"];
//               $res["phone"]=$data["phone"];
//               $res["create_time"]=$data["create_time"];
//               Db::name("project_stage_material")->insert($res);

   // $res = Db::name('yuyue')->insert(['name' => $data['name'], 'phone' =>  $data['phone'],'create_time'=>$data['create_time']]);


       }
        $weixin = new  Weixin();
        $url = request()->domain().$this->request->url();
        $js_config = $weixin->js_config($url);
        $this->assign('jsconfig',$js_config);
        return $this->fetch('online_order');
    }

    //测试例子
    public  function GetIpLookup(){
        if(empty($ip)){
            $ip=  $this->request->ip();
       }
        $res = @file_get_contents('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=',$ip);
        if(empty($res)){ return false; }
        $jsonMatches = array();
        preg_match('#\{.+?\}#', $res, $jsonMatches);
        if(!isset($jsonMatches[0])){ return false; }
        $json = json_decode($jsonMatches[0], true);
        if(isset($json['ret']) && $json['ret'] == 1){
            $json['ip'] = $ip;
            unset($json['ret']);
        }else{
            return false;
        }

        var_dump( $json);

    }


    //装修流程

    public  function  completeprocess(){
        return  $this->fetch('complete_process');
    }

    //案例
    public function cases(Request $request){
        if($request->isGet()){
            return $this->fetch();
        }

        if($request->isAjax()){
            $map=[];
            $city=$request->param("city");
            $house=$request->param("house");
            $area=$request->param("area");
            $page=$request->param("page");

            if($city!="不限"){
                $map['shi']=['=',$city];
            }
            if($house!="不限"){
                $map['house_type']=['=',$house];
            }
            if( $area!="不限"){
                if($area=="60平米以下"){
                    $map['area']=['<','60'];
                }
                if($area=="60-80平米"){
                    $map['area']=['between', '60,80'];
                }
                if($area=="80-100平米"){
                    $map['area']=['between', '80,100'];
                }
                if($area=="100平米以上"){
                    $map['area']=['>', '100'];
                }
            }

            if ($map){
                $res=Db::name("cases")
                    ->field('id,cases_name,house_type,area,small_img')
                    ->page($page,6)
                    ->where($map)
                    ->order('id', 'desc')
                    ->select();
            }
            else{
                $res=Db::name("cases")
                    ->field('id,cases_name,house_type,area,small_img')
                    ->page($page,6)
                    ->order('id', 'desc')
                    ->select();
            }


            $qiniu_config = cmf_get_option('qiniu');
            // 七牛样式处理
            $q_thumb="?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/crop/120x120/quality/100/interlace/0";
            foreach ($res as $index => $re) {
                $res[$index]['small_img'] = $qiniu_config['domain'] . '/' . $re['small_img'];
            }
            return json($res);
        }
    }


    public function lists(Request $request){
        $flag=$request->param("flag");
        if($flag==1){
            $res=[["name"=>"不限"],["name"=>"周口市"]];
            return json($res);
        }
        if($flag==2){
            // $res=array("不限","一居室","二居室","三居室","四居室","其他");
            $res=[["name"=>"不限"],["name"=>"一居室"],["name"=>"二居室"],["name"=>"三居室"],["name"=>"四居室"],["name"=>"其他"]];
            return json($res);
        }
        if($flag==3){
            //$res=array("不限","60平米以下","60-80平米","80-100平米","100平米以上");
            $res=[["name"=>"不限"],["name"=>"60平米以下"],["name"=>"60-80平米"],["name"=>"80-100平米"],["name"=>"100平米以上"]];
            return json($res);
        }

    }

    //案例详情
    public function cases_detail(Request $request)
    {
        //获取该项目id
        $id=$request->param("id");
        if (!$id) {
            $this->error('id 异常');
        }

        $slides = Db::name('cases_detail')->where('cases_id',$id)->select();
        foreach ($slides as $index => $re){
            $data = Db::name('cases')->where('id',$id)->find();
//            dump($data);
//            exit();
            $slides[$index]['cases_name']=$data['cases_name'];
            $slides[$index]['house_type']=$data['house_type'];
            $slides[$index]['area']=$data['area'];

        }
        //七牛域名
        $qiniu_config = cmf_get_option('qiniu');
        // 七牛样式处理
        $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/crop/60x60/quality/100/interlace/0";
        foreach ($slides as $index => $re) {
            $slides[$index]['cases_img'] = $qiniu_config['domain'] . '/' . $re['cases_img'] ;
        }


        $this->assign('data',$slides);
        $this->assign('pic_start', $slides[0]);
        $this->assign('pic_end', $slides[count($slides) - 1]);


        return $this->fetch('cases_detail');

    }

    //学装修详情
    public function xzx_detail(Request $request){
         //获取该项目id
        $id=$request->param("id");
        if (!$id) {
            $this->error('id 异常');
        }

        $res = Db::name('article_manage')->find($id);
//        var_dump($res);
//        exit();
        //七牛域名
        $qiniu_config = cmf_get_option('qiniu');
        // 七牛样式处理
        $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/crop/60x60/quality/100/interlace/0";
        $res['release_time'] = date('Y-m-d ', $res['release_time']);
        $res['small_img'] = $qiniu_config['domain'] . '/' . $res['small_img'] ;

        $this->assign('data', $res);

        return $this->fetch('xzx_detail');
    }

//    //下拉加载数据
//    public function loaddata(Request $request)
//    {
//        if ($request->isAjax()) {
//
//
//            $status[0] = '禁用';
//            $status[1] = '正常';
//            $page = input('page') ? input('page') : 1;
//            $limit = input('limit') ? input('limit') : 10;
//
//
//            $res = Db::name('cases')->page($page, $limit)->select();
//            $qiniu_config = cmf_get_option('qiniu');
//            // 七牛样式处理
//            $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/crop/80x120/quality/100/interlace/0";
//
//            foreach ($res as $index => $re) {
//                $res[$index]['small_img'] = $qiniu_config['domain'] . '/' . $re['small_img'] . $q_thumb;
//
//
//            }
//
//            return json(['code' => 1, 'msg' => 'ok', 'count' => count($res), 'data' => $res]);
//        }
//    }




}
